package kr.saeildamil.androidStudy_second.gesture;

import java.util.ArrayList;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;

public class GestureDump extends Activity {
	ArrayList<String> arGesture;
	TextView mResult;
	GestureDetector mDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arGesture = new ArrayList<String>();

		setContentView(R.layout.ch31_gesturedump);
		
		mResult = (TextView) findViewById(R.id.ch31_gesturedump_result);
		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setOnDoubleTapListener(mDouleTapListener);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	}

	OnDoubleTapListener mDouleTapListener = new OnDoubleTapListener() {
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			AppendText("onSingleTapConfirmed");
			return false;
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			AppendText("onDoubleTapEvent");
			return false;
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			AppendText("onDoubleTap");
			return false;
		}
	};

	OnGestureListener mGestureListener = new OnGestureListener() {
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			AppendText("onSingleTapUp");
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			AppendText("onShowPress");
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			String text = 
					String.format(
						"Scroll : (%d, %d) -> (%d, %d) (%d, %d)", 
						(int) e1.getX(),(int) e1.getY(), 
						(int) e2.getX(), (int) e2.getY(),
						(int) distanceX, (int) distanceY
					);
			AppendText(text);
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			AppendText("onLongPress");
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			String text = 
					String.format(
						"Fling : (%d, %d) -> (%d, %d) Velocity(%d, %d)",
						(int) e1.getX(),(int) e1.getY(), 
						(int) e2.getX(), (int) e2.getY(),
						(int) velocityX, (int) velocityY
					);
			AppendText(text);
			return false;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			String text = 
					String.format(
						"onDown : %d, %d", 
						(int)e.getX(), (int)e.getY()
					);
			AppendText(text);
			return false;
		}
	};

	private void AppendText(String text) {
		if (arGesture.size() > 15) {
			arGesture.remove(0);
		}
		arGesture.add(text);
		StringBuilder sb = new StringBuilder();
		for (String str : arGesture) {
			sb.append(str + "\n");
		}
		mResult.setText(sb.toString());
	}

}
